<?php
	class Company_Model extends CI_Model {
		function __construct()
		{
			parent::__construct();	
		}
		
		function get_company_lists($id=0,$page=1,$limit=5000,$gold=0,$agent=0,$ran=0,$start_plus=0){ //id=cat_id , $gold {0=all,1=general,2=gold} , $ran = random
			$start = (($page-1)*$limit)+$start_plus;
			
			$this->db->select('user_company_profile.*,users.modified');
			$this->db->from('user_company_profile');
			$this->db->join('users', 'user_company_profile.user_id = users.id');
			
			$this->db->where('users.active', 1);
			
			if($id!=0){
			    $this->db->where('user_company_profile.cat_id', $id);
			}
			
			if($gold==1){
				$time = time();
				$where = "(users.gold_active=0 OR users.gold_start >=$time OR users.gold_finish <=$time)";
				$this->db->where($where);
			}else if($gold==2){
				$this->db->where('users.gold_active', 1);
				$this->db->where('users.gold_start <=', time());
				$this->db->where('users.gold_finish >=', time());
				$this->db->order_by("gold_update", "DESC");
			}
			
			if($agent!=0){
			    $this->db->where('user_company_profile.agent', 1);
			}
			
			if($gold!=2){
				$this->db->order_by("user_company_profile.name", "ASC");
			}else{
				$this->db->order_by("users.modified", "DESC");
				$this->db->order_by("user_company_profile.c_modified", "DESC");
			}
			
			$query = $this->db->get();
			$result = $query->result();
			if($ran!=0){
				shuffle($result);
			}
			
			return $result;
		}
		
		function get_agent_lists($id=0,$page=1,$limit=5000){
			$start = ($page-1)*$limit;
			
			$this->db->select('user_company_profile.*,users.modified');
			$this->db->from('user_company_profile');
			$this->db->join('users', 'user_company_profile.user_id = users.id');
			
			$this->db->where('users.active', 1);
			$this->db->where('user_company_profile.agent', 1);
			
			if($id!=0){
			    $this->db->where('user_company_profile.cat_id', $id);
			}
			
			$this->db->order_by("modified", "DESC");
			$this->db->limit($limit , $start);
			
			$query = $this->db->get();
			return $query->result();
		}
		
		function search1($id=0,$page=1,$limit=5000,$gold=0,$agent=0,$ran=0,$key,$start_plus=0){
			$start = (($page-1)*$limit)+$start_plus;
			
			$this->db->select('user_company_profile.*,users.modified');
			$this->db->from('user_company_profile');
			$this->db->join('users', 'user_company_profile.user_id = users.id');
			
			$this->db->where('users.active', 1);
			
			if($id!=0){
			    $this->db->where('user_company_profile.cat_id', $id);
			}
			
			if($gold==1){
				$time = time();
				$where = "(users.gold_active=0 OR users.gold_start >=$time OR users.gold_finish <=$time)";
				$this->db->where($where);
			}else if($gold==2){
				$this->db->where('users.gold_active', 1);
				$this->db->where('users.gold_start <=', time());
				$this->db->where('users.gold_finish >=', time());
				$this->db->order_by("gold_update", "DESC");
			}
			
			if($agent!=0){
			    $this->db->where('user_company_profile.agent', 1);
			}
			
			if($gold!=2){
				$this->db->order_by("user_company_profile.name", "ASC");
			}else{
				$this->db->order_by("users.modified", "DESC");
				$this->db->order_by("user_company_profile.c_modified", "DESC");
			}
			
			if($key['subcat']!=0){
				$this->db->where('user_company_profile.cat_id', $key['subcat']);
			}else if($key['maincat']!=0){
				$this->db->where('user_company_profile.main_cat_id', $key['maincat']);
			}
			
			if($key['province']!=0){
				$this->db->where('user_company_profile.province', $key['province']);
			}elseif(isset($key['geography']) AND $key['geography']!=0){
				
				if(!is_array($key['geography'])){
					$this->db->where('user_company_profile.geo_id', $key['geography']);
				}elseif(is_array($key['geography'])){
					$this->db->where_in("user_company_profile.geo_id", $key['geography']);
				}
			}
			
			$query = $this->db->get();
			return $query->result();
		}
		
		function search2($id=0,$page=1,$limit=5000,$gold=0,$agent=0,$ran=0,$key,$start_plus=0){
			$start = (($page-1)*$limit)+$start_plus;
			
			$this->db->select('user_company_profile.*,users.modified');
			$this->db->from('user_company_profile');
			$this->db->join('users', 'user_company_profile.user_id = users.id');
			
			$this->db->where('users.active', 1);
			
			if($id!=0){
			    $this->db->where('user_company_profile.cat_id', $id);
			}
			
			if($gold==1){
				$time = time();
				$where = "(users.gold_active=0 OR users.gold_start >=$time OR users.gold_finish <=$time)";
				$this->db->where($where);
			}else if($gold==2){
				$this->db->where('users.gold_active', 1);
				$this->db->where('users.gold_start <=', time());
				$this->db->where('users.gold_finish >=', time());
				$this->db->order_by("gold_update", "DESC");
			}
			
			if($agent!=0){
			    $this->db->where('user_company_profile.agent', 1);
			}
			
			if($gold!=2){
				$this->db->order_by("user_company_profile.name", "ASC");
			}else{
				$this->db->order_by("users.modified", "DESC");
				$this->db->order_by("user_company_profile.c_modified", "DESC");
			}
			
			if($limit!=0){
				$this->db->limit($limit , $start);
			}
			
			$key_check = FALSE;
			if($key['search_text']==1){
				$where = '(1=1';
			}else{
				$where = '(1=0';
			}
			
			if($key['product_type']!='Enter Keyword'){
				$key_check = TRUE;
				$this->db->select('categories.name_th ,categories.name_en');
				$this->db->join('categories', 'user_company_profile.cat_id = categories.id');
				
				if($key['search_text']==1){
					$where .= " AND (categories.name_th LIKE '%{$key['product_type']}%' OR categories.name_en LIKE '%{$key['product_type']}%')";
				}else{
					$where .= " OR (categories.name_th LIKE '%{$key['product_type']}%' OR categories.name_en LIKE '%{$key['product_type']}%')";
				}
			}
			
			if($key['product_brand']!='Enter Keyword'){
				$key_check = TRUE;
				if($key['search_text']==1){
					$where .= " AND (user_company_profile.brand LIKE '%{$key['product_brand']}%')";
				}else{
					$where .= " OR (user_company_profile.brand LIKE '%{$key['product_brand']}%')";
				}
			}
			
			if($key['company_name']!='Enter Keyword'){
				$key_check = TRUE;
				if($key['search_text']==1){
					$where .= " AND (user_company_profile.name LIKE '%{$key['company_name']}%')";
				}else{
					$where .= " OR (user_company_profile.name LIKE '%{$key['company_name']}%')";
				}
			}
			
			$where .= ')';
			
			if($key_check){
				$this->db->where($where);
			}
			
			$this->db->distinct();
			$query = $this->db->get();
			return $query->result();
		}
	}


/* End of file company_model.php */
/* Location: ./application/models/admin/company_model.php */